/*
 * Copyright (C) Francisco Morero Peyrona. All rights reserved.
 *
 * This software is published under the terms of Open Source 
 * License version 1.1, a copy of which has been included with this
 * distribution in the License.txt file.  
 */

package jdbcmanager.table;

import java.util.*;
import java.lang.reflect.*;
import java.sql.*;

import java.awt.*;
import javax.swing.*;

import jdbcmanager.app.*;
import jdbcmanager.dbtools.datatypes.JDBCTypeConverter;
import jdbcmanager.swing.*;
import jdbcmanager.swing.table.*;

/**
 * 
 * @author Francisco Morero Peyrona
 */
final class ResultSetInfo extends JxDialog
{
	public ResultSetInfo( ResultSetMetaData rsmd )
	{
		super( "ResultSet MetaData Information" );

		setVisibleButtonPanel( false );

		if( rsmd != null )
		{
			App.setCursor( Cursor.WAIT_CURSOR );
			init( rsmd );
			App.restoreCursor();
		}

		if( rsmd == null )
			getContentPane().add( new JLabel( "Information not available.", SwingConstants.CENTER ) );
	}

	private void init( ResultSetMetaData rsmd )
	{
		try
		{
			Vector   vHead        = new Vector();
			Vector   vData        = new Vector();
			Method[] aMethod      = rsmd.getClass().getDeclaredMethods();
			int      nCols        = rsmd.getColumnCount();
			int      nColCountPos = -1;  // "getColCount()" index in aMethod[]
			int      nColNamePos  = -1;  // "getColName()"  index in aMethod[]

			vHead.add( "Column" ); // Add column name head
			
			for( int n = 0; n < aMethod.length; n++ )
			{
				if( aMethod[n].getName().equals( "getColumnCount" ) )     // Method not used
					nColCountPos = n + 1;
				else if( aMethod[n].getName().equals( "getColumnName" ) ) // Method not used
					nColNamePos = n + 1;
				else
					vHead.add( aMethod[n].getName() );
			}

			for( int n = 1; n <= nCols; n++ )    // Columns are 1 based (not 0 based)
			{
				Vector vRow = new Vector( nCols );
				vRow.add( rsmd.getColumnName( n ) );

				for( int m = 0; m < aMethod.length; m++ )
				{
					if( m != nColCountPos && m != nColNamePos )
					{
						try
						{
							Object[] aoArgs = { new Integer( n ) };
							vRow.add( aMethod[m].invoke( rsmd, aoArgs ) );
						}
						catch( Exception exc )
						{
							vRow.add( "<Unknown>" );
						}
					}
				}
				
				vData.add( vRow );
			}
			
            // It is needed to check because some drivers do not provide all the information they are supouse to provide 
            if( vData.size() > 0 &&  ((Vector) vData.get( 0 )).size() >= 16  )
            {
    			// As it is known that in index 16 is stored the ColumType, I change
    			// the number by a more informative information (a name)
    			for( int n = 0; n < vData.size(); n++ )
    			{
    				Vector vRow  = (Vector) vData.get( n );
    				int    nType = ((Integer) vRow.get( 16 )).intValue();
    				String sType = JDBCTypeConverter.getJdbcTypeName( nType );
    				       
    				vRow.set( 16, sType );
    			}
            }

			JxTableVector table = new JxTableVector( vData, vHead );
			table.adjustColumnsWidth();

			getContentPane().setLayout( new BorderLayout() );
			getContentPane().add( new JScrollPane( table ), BorderLayout.CENTER );
		}
		catch( Exception exc )
		{
			App.showDebugException( exc );
			rsmd = null; // Checked by the constructor
		}
	}
}